﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Business;
using System.Drawing;

public partial class client_TechnicalDataSheets : @base
{
    DataTable components = new DataTable();
    ReportServices rs = new ReportServices();
    DataSheetService ds = new DataSheetService();
   // mshtml.HTMLWindow2Class window;
    protected void Page_Load(object sender, EventArgs e)
    {

        //for test
       

        if (UserDetails == null)
        {
            string jscript = "<script language='javascript'  type='text/javascript'> parent.document.location.href= '../Default.aspx'</script>";
            //Page.RegisterClientScriptBlock("key2ClientBlock", jscript);
            this.ClientScript.RegisterClientScriptBlock(this.GetType(), "key3ClientBlock", jscript);

        }
        if (!IsPostBack)
        {
            if (UserDetails.Role != "Admin")
            {
                TaskGridView.AutoGenerateDeleteButton = false;
                TaskGridView.AutoGenerateEditButton = false;
                Label1.Visible = false;
                Label2.Visible = false;
                txtAttribute.Visible = false;
                txtValue.Visible = false;
                Get_Button.Visible = false;
                save.Visible = false;
                CategoryManage.Visible = false;
                

            }
            
            MachineNameLabel.Text = "Technical Data Sheet for " + MACHINENAME;
            components = rs.getcomponentsByMachine(MACHINEID);
            foreach (DataRow component in components.Rows)
            {
                String componentName = component["componentName"].ToString();
                string componentID = component["component_id"].ToString();

                System.Web.UI.WebControls.ListItem ls = new System.Web.UI.WebControls.ListItem(componentName, componentID, true);
                DropDownClientList.Items.Add(ls);
            }
            Session["TaskTable"] = getTable();

            //Bind data to the GridView control.
            BindData();
            

        }
    }
    protected void Component_Changed(object sender, EventArgs e)
    {
       
        Session["TaskTable"] = getTable();
        BindData();
    }
    protected DataTable getTable()
    {
       // Create a new table.
        DataTable taskTable = new DataTable("TaskList");

        // Create the columns.
        taskTable.Columns.Add("Attribute", typeof(string));
        taskTable.Columns.Add("Value", typeof(string));
        string component_id = DropDownClientList.SelectedValue.ToString();
        taskTable = ds.getDataByComponentID(component_id);
        //Persist the table in the Session object.
        if (taskTable.Rows.Count == 0)
        {
           string type = ds.getComponentType(component_id);
           DataTable temple = new DataTable();
           temple = ds.getDataByTypeID(type);
           foreach (DataRow r in temple.Rows)
           {
               DataRow tableRow = taskTable.NewRow();
               tableRow["Attribute"] = r["attribute"].ToString();
               tableRow["Value"] = "";
               taskTable.Rows.Add(tableRow);
           }

        }
        return taskTable;
    }
  /*  protected void TaskGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        TaskGridView.PageIndex = e.NewPageIndex;
        //Bind data to the GridView control.
        BindData();
    }*/

    protected void TaskGridView_RowEditing(object sender, GridViewEditEventArgs e)
    {
        //Set the edit index.
        TaskGridView.EditIndex = e.NewEditIndex;
        //Bind data to the GridView control.
        BindData();
    }

    protected void TaskGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        //Reset the edit index.
        TaskGridView.EditIndex = -1;
        //Bind data to the GridView control.
        BindData();
    }

    protected void TaskGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //Retrieve the table from the session object.
        DataTable dt = (DataTable)Session["TaskTable"];
        
        //Update the values.
        GridViewRow row =  TaskGridView.Rows[e.RowIndex];
        dt.Rows[row.DataItemIndex]["Attribute"] = ((TextBox)(row.FindControl("TextBox1"))).Text;
        dt.Rows[row.DataItemIndex]["Value"] = ((TextBox)(row.FindControl("TextBox2"))).Text;//((TextBox)(row.Cells[2].Controls[0])).Text;        
        
        /*
        dt.Rows[e.RowIndex]["Attribute"] = e.NewValues["Attribute"].ToString();
        dt.Rows[e.RowIndex]["Value"] = e.NewValues["Value"].ToString();*/
        Session["TaskTable"] = dt;
        //Reset the edit index.
        TaskGridView.EditIndex = -1;

        //Bind data to the GridView control.
        BindData();
    }
    protected void TaskGridView_RowDeleting(Object sender, GridViewDeleteEventArgs e)
    {
        DataTable dt = (DataTable)Session["TaskTable"];
        int k = dt.Rows.Count;
         GridViewRow row = TaskGridView.Rows[e.RowIndex];

         string att =((TextBox) (row.Cells[1].FindControl("TextBox1"))).Text.ToString();
        
         if (att.ToUpper() == "UIN")
         {
             string jscript = "<script language='javascript'>alert('Cannot delete such attribute!')</script>";
             // Page.RegisterClientScriptBlock("keyClientBlock",jscript);
             this.ClientScript.RegisterClientScriptBlock(this.GetType(), "keyClientBlock", jscript);
        
         }
         else
         {
             // dt.Rows[row.DataItemIndex].Delete();
             dt.Rows.RemoveAt(e.RowIndex);
             k = dt.Rows.Count;         
             Session["TaskTable"] = dt;
             BindData();
         }
    }
    protected void TaskGridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int i = e.Row.DataItemIndex;
            e.Row.Cells[0].ForeColor = Color.Blue;
             TextBox tb1 = (TextBox)e.Row.FindControl("TextBox1");
             tb1.Text = ((DataTable)(Session["TaskTable"])).Rows[i]["Attribute"].ToString();
             
             // tb1.Text = showData.Rows[i]["Attributes"].ToString();
             TextBox tb2 = (TextBox)e.Row.FindControl("TextBox2");
             tb2.Text = ((DataTable)(Session["TaskTable"])).Rows[i]["Value"].ToString();
             if (tb1.Text == "UIN")
             {
                 tb1.ReadOnly = true;
                // tb1.Visible = false;
                 tb2.ReadOnly = true;
                // tb2.Visible = false;
             }
        }

    }
    private void BindData()
    {
        TaskGridView.DataSource = Session["TaskTable"];
        TaskGridView.DataBind();
    }


   
    protected void Button1_Click(object sender, EventArgs e)
    {
        
        
        DataTable dt = (DataTable)Session["TaskTable"];
        int k = dt.Rows.Count;
        string component_id = DropDownClientList.SelectedValue.ToString();
        ds.updateComponentTechnicalData(dt,component_id);
        string jscript = "<script language='javascript'>alert('Edit successfully!')</script>";
        //Page.RegisterClientScriptBlock("key2ClientBlock", jscript);
        this.ClientScript.RegisterClientScriptBlock(this.GetType(), "key2ClientBlock", jscript);
           
    }
 /*   public void setPage(mshtml.HTMLWindow2Class JSFile1,mshtml.HTMLWindow2Class JSFile2 )
    {
        window = JSFile;
    }*/
    protected void AddAttribute(object sender, EventArgs e)
    {
        string attribute=txtAttribute.Text.ToString();
        string value = txtValue.Text.ToString();
        
        if (attribute == "") 
        {
            
            string jscript = "<script language='javascript'>alert('Should input name of attribute')</script>";
           // Page.RegisterClientScriptBlock("keyClientBlock",jscript);
            this.ClientScript.RegisterClientScriptBlock(this.GetType(), "keyClientBlock", jscript);
        }
        else
        {
            if (attribute.ToUpper() == "UIN")
            {
                string jscript = "<script language='javascript'>alert('Cannot add such attribute')</script>";
                // Page.RegisterClientScriptBlock("keyClientBlock",jscript);
                this.ClientScript.RegisterClientScriptBlock(this.GetType(), "keyClientBlock", jscript);

            }
            else
            {
                int ok = 1;
                DataTable dt = (DataTable)Session["TaskTable"];    
                foreach (DataRow r in dt.Rows)
                {
                    string att = r["Attribute"].ToString();
                    if (attribute == att)
                    {
                        ok = 0;
                        break;
                    }
                }
                if (ok == 0)
                {
                    string jscript = "<script language='javascript'>alert('This attribute already exists!')</script>";
                    // Page.RegisterClientScriptBlock("keyClientBlock",jscript);
                    this.ClientScript.RegisterClientScriptBlock(this.GetType(), "keyClientBlock", jscript);

                }
                else
                {
                    DataRow newAttribute = dt.NewRow();
                    newAttribute["Attribute"] = attribute;
                    newAttribute["Value"] = value;

                    dt.Rows.Add(newAttribute);
                    Session["TaskGridView"] = dt;
                    txtAttribute.Text = "";
                    txtValue.Text = "";
                }
            }
        }
        BindData();

    }
    protected void CategoryManage_Click(object sender, EventArgs e)
    {
        Server.Transfer("ManageCategory.aspx");
    }
    protected void viewTrend_Click(object sender, EventArgs e)
    {
        Server.Transfer("TrendReport.aspx");
    }
    protected void TaskGridView_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}
